<template>
  <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <!-- - 对于el-option组件，它有两个重要的属性：
        label: 选项显示的内容
- value: 用户选中这一项之后，会保存到v-model中的值 -->
        <el-option v-for="item in employees" :key="item.id" :value="item.username" :label="item.username" />

      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>

</template>
<script>
import { getEmployeeList } from '@/api/employees'
import { addDepartments } from '@/api/depat'
import { getDepartDetail, updateDepartments } from '@/api/departments'
export default {
  props: {
    id: {
      type: Number,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },

  data() {
    const validatecode = (rules, value, callback) => {
      const codeList = this.originList.map((item) => { return item.code })
      if (codeList.includes(value)) {
        callback(new Error('编码已存在'))
      } else {
        callback()
      }
    }
    return {

      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      employees: [],
      rules: {
        name: [{ required: true, message: '请输入名字', trigger: 'blur' }, { min: 1, max: 10, message: '请输入1到10位的名字', trigger: 'blur' }],
        code: [{ required: true, message: '请输入编码', trigger: 'blur' }, { min: 1, max: 10, message: '请输入1到10位的编码', trigger: 'blur' }, {
          validator: validatecode, trigger: 'blur'
        }],
        manager: [{ required: true, message: '请输入管理者', trigger: 'blur' }],
        introduce: [{ required: true, message: '请输入部门介绍', trigger: 'blur' }, { min: 1, max: 300, message: '请输入1到300位的介绍', trigger: 'blur' }]
      }
    }
  },
  watch: {
    'id': function(newVal, oldVal) {
      console.log('id变了！ 新值：', newVal, '旧值', oldVal)
      // 重发请求获取详情
      this.loadDepartmentDetailById()
    } },
  created() {
    this.loadEmployeeSimple()
    if (this.isEdit) {
      this.loadDetail()
    }
  },
  methods: {
    async doEdit() {
      try {
        await updateDepartments(this.id)
        this.$message({ type: 'success', message: '修改成功' })
        this.$emit('success')
      } catch (err) {
        console.log(err)
        this.$message({ type: 'error', message: err })
      }
    },

    // 获取当前部门的详情
    async loadDetail() {
      if (this.isEdit) {
        // alert('现在是编辑，要去获取详情')
        const res = await getDepartDetail(this.id)
        console.log('现在是编辑，要去获取详情', res)
        this.form = res.data
      }
    },
    //
    async loadEmployeeSimple() {
      const res = await getEmployeeList()
      console.log(res)
      this.employees = res.data
    },

    async doAdd() {
      const params = { ...this.form, pid: this.id }
      const res = await addDepartments(params)
      console.log(res)
      this.$emit('success')
    },
    // 确定
    hSubmit() {
      this.$refs.deptForm.validate((vaild) => {
        if (!vaild) return // 失败
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    // 取消
    hCancel() {
      // 通知父组件去关闭弹层
      this.$emit('close')
    }
  }
}

</script>
